<template >
    <a-button v-if="record.status == 2" type="primary" size="small" @click.stop="writeOffPass()" v-auth="props.auth"><icon-check />核销</a-button>
    <add-modal ref="addModalRef"></add-modal>
</template>

<script setup lang="ts">
import {computed, ref,inject,watch,reactive} from "vue";
import AddModal from "@cps/actions/addModal.vue";
import customerBill from '@/api/finance/customerBill'
const addModalRef = ref()
const record = inject('record')
const props = defineProps({
  deduction: {
        default: false
    },
    auth: {
        type: Array,
        default: []
    },
    api: {
        type: Function,
        default: ()=>{}
    },
    call: {
        type: Function,
        default: ()=>{}
    },
})
const writeOffPass = () => {
  addModalRef.value.open(reactive([
      {
        title: "账号",
        dataIndex: "account_id",
        formType: "select",
        addDefaultValue:record.value.account_id,
        // disabled:true,
        required: true,
        rules: {
          required: true,
          message: "请选择账号"
        },
        dict: {
          url: 'finance/financialAccount/list',
          props: {label: 'name', value: 'id'} ,
          params: {select: 'id,name',user:record.value.user_id,id:record.value.account_id,currency:record.value.currency},
        }, 
      },
      {
        title: "核销金额",
        // disabled:true,
        dataIndex: "amount",
        addDefaultValue:record.value.unaudit_amount,
        formType: "input",
        required: true,
        rule: {
          required: true,
          message: "请填写金额"
        },
      },
      {
        title: "汇率",
        // disabled:true,
        dataIndex: "exchange_rate",
        formType: "input",
      },
      {
        formType: "checkbox",
        // disabled:true,
        addDisplay:props.deduction,
        dataIndex: "deduction_check",
        dict:{
          data:[
            {label:'尾款冲账',value:1},
          ]
        },
        onChange:(value,maFormObject)=>{
          let formData = maFormObject.formModel.value
          if(value.length > 0){
            formData.deduction = record.value.unaudit_amount - formData.amount
          }else{
            formData.deduction = 0
          }
          formData.deduction = formData.deduction > 0 ? formData.deduction : 0
        }
      },
      {
        title: "冲账金额",
        formType: "input",
        disabled:true,
        dataIndex: "deduction",
        addDisplay:props.deduction,
        addDefaultValue:0,
      },
      {
        title: "备注",
        formType: "textarea",
        dataIndex: "remark",
      },
      {
        title: "id",
        formType: "input",
        dataIndex: "id",
        addDefaultValue:record.value.id,
        addDisplay:false,
      },
    ]) , {
         viewType: 'modal',
      }, props.api,'核销',function name() {
        props.call()
      })
}

</script>